package com.dongdongshop.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.Goods;
import com.dongdongshop.pojo.Item;
import com.dongdongshop.service.ItemService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(consumerGroup = "ljq2202-consumer", messageModel = MessageModel.CLUSTERING, topic = "goods-score")
public class MqCousnm implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ItemService itemService;

    @Override
    public void onMessage(String s) {
        System.out.println("消费成功：" + s);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {


                MessageExt messageExt = list.get(0);
                int reconsumeTimes = messageExt.getReconsumeTimes();
                try {

                    messageExt.getTopic();
                    String msgId = messageExt.getMsgId();
                    Boolean hasKey = stringRedisTemplate.hasKey("MESSAGE_TX_" + msgId);
                    if (!hasKey) {
                        byte[] body = messageExt.getBody();
                        String s = new String(body);
//                    System.out.println(s);
                        JSONObject userJson = JSONObject.parseObject(s);
                        Item item = JSON.toJavaObject(userJson, Item.class);
                        System.out.println(item);
                        itemService.updateItem(item);
                        stringRedisTemplate.boundValueOps("MESSAGE_TX_" + msgId).set("已消费", 2, TimeUnit.MINUTES);
                    }

                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

                } catch (Exception e) {
                    e.printStackTrace();
                    if (reconsumeTimes < 2) {
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            }
        });
    }
}
